﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;
using System.Globalization;

namespace WpfImageBroadcaster
{
    public class ParameterCollection
    {
        public bool Not { get; set; }
    }

    public class FancyVisibilityConverter : IValueConverter
    {
        private object VisibilityToBool(object value, bool not)
        {
            if (!(value is Visibility))
            {
                return DependencyProperty.UnsetValue;
            }

            return (((Visibility)value) == Visibility.Visible) ^ not;
        }

        private object BoolToVisibility(object value, bool not)
        {
            if (!(value is bool))
            {
                return DependencyProperty.UnsetValue;
            }

            return ((bool)value ^ not) ? Visibility.Visible : Visibility.Collapsed;
        }

        private ParameterCollection ReadParameters(object parameter)
        {
            ParameterCollection results = new ParameterCollection();

            string parameterString = parameter as string;

            if (string.IsNullOrEmpty(parameterString))
            {
                return results;
            }

            var parameters = parameterString.Split(',');

            for (int n = 0; n < parameters.Length; n++)
            {
                HandleParameter(parameters[n].Replace(",", "").Trim(), results);
            }

            return results;
        }

        private void HandleParameter(string parameter, ParameterCollection results)
        {
            if (parameter.Equals("NOT"))
            {
                results.Not = true;
            }
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var parameters = ReadParameters(parameter);

            if (value is bool)
            {
                return BoolToVisibility(value, parameters.Not);
            }
            else if (value is Visibility)
            {
                return VisibilityToBool(value, parameters.Not);
            }
            else if (value == null)
            {
                return parameters.Not ? Visibility.Visible : Visibility.Collapsed;
            }
            else if (value is string)
            {
                // test this one
                return BoolToVisibility(!string.IsNullOrEmpty(value.ToString()), parameters.Not);
            }

            return DependencyProperty.UnsetValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var parameters = ReadParameters(parameter);

            if (value is bool)
            {
                return BoolToVisibility(value, parameters.Not);
            }
            else if (value is Visibility)
            {
                return VisibilityToBool(value, parameters.Not);
            }
            else if (value == null)
            {
                return parameters.Not ? true : false;
            }
            else if (value is string)
            {
                // test this one
                return BoolToVisibility(!string.IsNullOrEmpty(value.ToString()), parameters.Not);
            }

            return DependencyProperty.UnsetValue;
        }
    }
}
